package hr.ctg.domain.action.korisnik;

import hr.ctg.domain.model.Dogadjaj;
import hr.ctg.domain.model.DojamPosjetitelja;
import hr.ctg.domain.model.Korisnik;
import hr.ctg.domain.model.Objava;
import hr.ctg.domain.model.Objekt;
import hr.ctg.domain.model.Rola;
import hr.ctg.domain.model.Slika;
import hr.ctg.service.dao.DogadjajDAO;
import hr.ctg.service.dao.DojamPosjetiteljaDAO;
import hr.ctg.service.dao.KorisnikDAO;
import hr.ctg.service.dao.ObjavaDAO;
import hr.ctg.service.dao.ObjektDAO;
import hr.ctg.service.dao.SlikaDAO;

import java.util.List;

import javax.faces.application.FacesMessage;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.security.Identity;

@Name("profileAction")
@Scope(ScopeType.CONVERSATION)
public class ProfileAction {
	@In
	ObjektDAO objektDAO;
	@In
	KorisnikDAO korisnikDAO;
	@In
	SlikaDAO slikaDAO;
	@In
	ObjavaDAO objavaDAO;
	@In
	DogadjajDAO dogadjajDAO;
	@In
	DojamPosjetiteljaDAO dojamPosjetiteljaDAO;

	@In
	FacesMessages facesMessages;
	@In
	Identity identity;

	@In(required = false)
	Korisnik korisnik;

	@SuppressWarnings("unused")
	@org.jboss.seam.annotations.datamodel.DataModel
	private List<Objava> objaveByKorisnik;

	@SuppressWarnings("unused")
	@org.jboss.seam.annotations.datamodel.DataModel
	private List<Dogadjaj> dogadjajiByKorisnik;

	@SuppressWarnings("unused")
	@org.jboss.seam.annotations.datamodel.DataModel
	private List<DojamPosjetitelja> dojmoviByKorisnik;

	@SuppressWarnings("unused")
	@org.jboss.seam.annotations.datamodel.DataModel
	private List<Objekt> oglasiByKorisnik;

	@SuppressWarnings("unused")
	@org.jboss.seam.annotations.datamodel.DataModel
	private List<Objekt> favoritiByKorisnik;

	private byte[] logo;
	private String tipLoga;

	@Transactional
	public void updateUserData() {
		try {
			korisnikDAO.update(korisnik);
			korisnikDAO.flush();
		} catch (final Exception e) {
			facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR,
					"message.update.unsuccessfull", new Object[0]);

			return;
		}
		facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO,
				"message.update.successfull", new Object[0]);
	}

	@Factory("oglasiByKorisnik")
	public void getOglasi() {
		if (identity.hasRole(Rola.Role.POSLOVNI_OGLASIVAC.toString())
				|| identity.hasRole(Rola.Role.PRIVATNI_OGLASIVAC.toString())) {
			oglasiByKorisnik = objektDAO.findByIdOglasivaca(korisnik
					.getIdKorisnika());
		} else {
			oglasiByKorisnik = objektDAO.findByIdAutora(korisnik
					.getIdKorisnika());
		}
	}

	@Factory("favoritiByKorisnik")
	public void getFavoriti() {
		favoritiByKorisnik = objektDAO.findByIdMotritelja(korisnik
				.getIdKorisnika());
	}

	@Factory("slikeByKorisnik")
	public List<Objekt> getSlike() {
		return objektDAO.findByKorisnikSlike(korisnik);
	}

	public List<Slika> getSlike(final Objekt objekt) {
		return slikaDAO.findByKorisnikObjekt(korisnik, objekt);
	}

	public byte[] getLogo() {
		return logo;
	}

	public void setLogo(final byte[] logo) {
		this.logo = logo;
	}

	public String getTipLoga() {
		return tipLoga;
	}

	public void setTipLoga(final String tipLoga) {
		this.tipLoga = tipLoga;
	}

	public void addLogo() {
		if (logo != null) {
			korisnik.setLogo(logo);
			korisnik.setTipLoga(tipLoga);
		}
		// FacesContext.getCurrentInstance().renderResponse();
	}

	@Factory("objaveByKorisnik")
	public void getObjave() {
		objaveByKorisnik = objavaDAO.findByIdKorisnika(korisnik
				.getIdKorisnika());
	}

	@Factory("dogadjajiByKorisnik")
	public void getDogadjaji() {
		dogadjajiByKorisnik = dogadjajDAO.findByIdKorisnika(korisnik
				.getIdKorisnika());
	}

	@Factory("dojmoviByKorisnik")
	public void getDojmovi() {
		dojmoviByKorisnik = dojamPosjetiteljaDAO.findByIdKorisnika(korisnik
				.getIdKorisnika());
	}
}